package de.fhd.pms.dao;

import java.util.Date;
import java.util.List;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Before;
import org.junit.Test;

import de.fhd.pms.model.Action;
import de.fhd.pms.model.Tour;
import de.fhd.pms.model.User;

/**
 * A Test class (Junit) for {@link TourDao} to find out if all Methods defined in TourDao are working fine.
 * This class uses a special test-database "hitchtest".
 */
public class TourDaoTest extends AbstractDataAccessTest {

	private String tableName = "tour";
	private String[] tables = { tableName };

	private static final Log log = LogFactory.getLog(UserDaoTest.class);

	private static final String[] configFiles = new String[] { "test-application-context.xml" };

	Date birthdate = null;
	
	User user = null;
	Action action = null;
	Tour tour = null;
	TourDao tourDao = null;
	UserDao userDao = null;
	ActionDao actionDao = null;
	
	/**Set TourDao
	 @param tourDao
	  */
	public void setTourDao(TourDao tourDao) {
		this.tourDao = tourDao;
	}
	/**Set UserDao
	 @param userDao
	  */
	public void setUserDao(UserDao userDao){
		this.userDao = userDao;
	}
	
	/**Set ActionDao
	 @param actionDao
	  */
	public void setActionDao(ActionDao actionDao) {
		this.actionDao = actionDao;
	}

	/**Setup Configuration
	  */
	@Before
	public void onSetUp() {
		
		Date date = new Date();
		
		user = new User();
		user.setEnabled(1);
		user.setBirthdate(birthdate);
		user.setGender(1);
		user.setEmail("test@test.lv");
		user.setUsername("andreas");
		user.setPassword("qwER12!@");
		user.setPasswordConfirm("qwER12!@");
		user.setName("Andreas");
		user.setLastname("Janow");
		
		action = new Action();
		action.setDescription("Some small description");
		action.setEnd_date(date);
		action.setEnd_hours(11);
		action.setEnd_minutes(10);
		action.setName("Action name");
		action.setStart_date(date);
		action.setStart_hours(10);
		action.setStart_minutes(10);
		action.setUser(user);
		
		
		tour = new Tour();
		tour.setAction(action);
		tour.setUser(user);
		tour.setEnd_hours(10);
		tour.setEnd_minutes(10);
		tour.setMax_member_count(2);
		tour.setMin_member_count(1);
		tour.setDescription("description");
		tour.setStart_date(date);
		tour.setEnd_hours(10);
		tour.setEnd_minutes(10);
		
	}
	/**
	 * A Test to find out if the {@link Tour} can be created depending on {@link User} and {@link Action}.
	 */
	@Test
	public void testFindById() {
		
		userDao.save(user);
		actionDao.save(action);
		tourDao.save(tour);
		
		Integer id = tour.getId();
		
		Tour testTour = tourDao.findByTourId(id);
		assertEquals(tour.getId(), testTour.getId());
	
		tourDao.deleteAll();
		actionDao.delete(action);
	}
	/**
	 * A Test to find out if more Tours can be created depending on {@link User} and {@link Action}.
	 */
	@Test
	public void testFindAll(){
		userDao.save(user);
		actionDao.save(action);
		tourDao.save(tour);
		tour.setId(null);
		tourDao.save(tour);
		
		List<Tour> tours = tourDao.findAll();
		System.out.println(tours);
		assertEquals(2, tours.size());
		tourDao.deleteAll();
		actionDao.delete(action);
		userDao.delete(user);
	}
	/**
	 * A Test to find out if the {@link} Tour can be saved depending on {@link} User and {@link} Action.
	 */
	@Test
	public void testSave(){
		Tour testTour;
		userDao.save(user);
		actionDao.save(action);
		tourDao.save(tour);
		
		Set<Tour> testTours = tourDao.findByUser(user);
		testTour = null;
		for (Tour t : testTours) {
			testTour = t;
		}
		
		assertEquals(tour.getId(), testTour.getId());
		
		tourDao.deleteAll();
		actionDao.delete(action);
		userDao.delete(user);	
	} 
	
	
}
